// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package storage

import (
	"strings"
	"unicode/utf8"

	"code.geniusai.org/notebook-manager/pkg/entity"
)

const (
	MaxKeywordLen = 39
)

// TODO
func ProcessKeyword(keyword string) (string, error) {
	trimSpace := strings.TrimSpace(keyword)
	if utf8.RuneCountInString(trimSpace) > MaxKeywordLen {
		return "", entity.NewError(entity.CodeParamInvalid, entity.KeyWordTooLong)
	}
	ans := "%"
	for _, c := range trimSpace {
		switch c {
		case '_':
			ans += "\\_"
		case '%':
			ans += "\\%"
		case '\\':
			ans += "\\\\"
		default:
			ans += string(c)
		}
	}
	ans += "%"
	return ans, nil
}
